package com.fortune.controller.app;

import com.faneui.common.bean.BaseResp;
import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.ListUtils;
import com.fortune.dal.dao.AccountDAO;
import com.fortune.dal.dao.BankCardDAO;
import com.fortune.dal.dao.BankDAO;
import com.fortune.dal.dao.CustDAO;
import com.fortune.dal.model.Account;
import com.fortune.dal.model.Bank;
import com.fortune.dal.model.BankCard;
import com.fortune.dal.model.Cust;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("bindcard")
public class BindCardController {

    @Autowired
    private HttpSession httpSession;

    @Autowired
    private CustDAO custDAO;

    @Autowired
    private AccountDAO accountDAO;

    @Autowired
    private BankDAO bankDAO;

    @Autowired
    private BankCardDAO bankCardDAO;

    //注册时，首次绑定银行卡
    @RequestMapping(value = "bindcardfir", method = RequestMethod.GET)
    public ModelAndView indexOfAllOne() throws BusinessException {
        ModelAndView mv = new ModelAndView("bindcardfir");
        mv.addObject("menuFlag", "bindcardfir");
        return mv;
    }

    @RequestMapping(value = "bindcardsec", method = RequestMethod.GET)
    public ModelAndView indexOfAllTwo() throws BusinessException {
        ModelAndView mv = new ModelAndView("bindcardsec");
        mv.addObject("menuFlag", "bindcardsec");
        return mv;
    }

    @RequestMapping(value = "bindcardthir", method = RequestMethod.GET)
    public ModelAndView indexOfAllThree() throws BusinessException {
        ModelAndView mv = new ModelAndView("bindcardthir");
        mv.addObject("menuFlag", "bindcardthir");
        return mv;
    }

    @RequestMapping(value = "bindcardfour", method = RequestMethod.GET)
    public ModelAndView indexOfAllFour() throws BusinessException {
        ModelAndView mv = new ModelAndView("bindcardfour");
        mv.addObject("menuFlag", "bindcardfour");
        return mv;
    }

    @RequestMapping(value = "addTradePassword", method = RequestMethod.POST)
    public BaseResp addTradePassword(@RequestBody Cust cust) throws BusinessException {
        BaseResp resp = new BaseResp();
        try{
            if(StringUtils.isBlank(cust.getTradePassword())){
                throw new BusinessException("输入交易密码不能为空");
            }
            Cust param = new Cust();
            param.setPhone((String) httpSession.getAttribute("phone"));
            List<Cust> list = custDAO.queryByModel(param);
            if(ListUtils.isEmpty(list)){
                throw new BusinessException("未找到用户信息");
            }
            Cust cust1 = list.get(0);
            cust1.setTradePassword(cust.getTradePassword());
            custDAO.updateById(cust1);
        }catch(Exception ex){
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }

    @RequestMapping(value = "bindcardsuccess", method = RequestMethod.GET)
    public ModelAndView indexOfAllPass() throws BusinessException {
        ModelAndView mv = new ModelAndView("bindcardsuccess");


        Cust param = new Cust();
        param.setPhone((String)httpSession.getAttribute("phone"));
        List<Cust> list = null;
        try {
            list = custDAO.queryByModel(param);
            if(ListUtils.isEmpty(list)){
                throw new BusinessException("未找到新增的数据");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        Cust cust = list.get(0);
        httpSession.setAttribute("user",cust);
        Account account = new Account();
        account.setCustId(cust.getId());
        account.setAccountBalance(0d);
        try {
            accountDAO.insert(account);
        } catch (Exception e) {
            e.printStackTrace();
        }

        mv.addObject("menuFlag", "bindcardsuccess");
        return mv;
    }





    //用户中心 绑定新的银行卡
    @RequestMapping(value = "bindnewcardone", method = RequestMethod.GET)
    public ModelAndView BindNewCardOne() throws BusinessException {
        ModelAndView mv = new ModelAndView("bindnewcardone");
        mv.addObject("bindnewcardone", "bindnewcardone");
        return mv;
    }

    //将填写的卡号 存在session中  并判断是否选择了银行
    @RequestMapping(value = "addcardcode", method = RequestMethod.POST)
    public BaseResp addCardCode(@RequestBody BankCard bankCard) throws BusinessException {
        BaseResp resp = new BaseResp();
        try {
            httpSession.setAttribute("cardCode", bankCard.getCardCode());
            if (httpSession.getAttribute("bankName") == null) {
                throw new BusinessException("未选择所属银行，请选择");
            }
        }catch(Exception ex){
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }

    @RequestMapping(value = "bindnewcardtwo", method = RequestMethod.GET)
    public ModelAndView BindNewCardTwo() throws BusinessException {
        ModelAndView mv = new ModelAndView("bindnewcardtwo");
        mv.addObject("menuFlag", "bindnewcardtwo");
        return mv;
    }

    @RequestMapping(value = "bindnewcardthree", method = RequestMethod.GET)
    public ModelAndView BindNewCardThree() throws BusinessException {
        ModelAndView mv = new ModelAndView("bindnewcardthree");
        mv.addObject("menuFlag", "bindnewcardthree");
        return mv;
    }

    //输入短信验证码  验证成功后 添加银行卡到数据库
    @RequestMapping(value = "bindnewcardfour", method = RequestMethod.GET)
    public ModelAndView BindNewCardFour() throws BusinessException {
        ModelAndView mv = new ModelAndView("bindnewcardfour");

        BankCard bankCard = new BankCard();
        String bankName = (String)httpSession.getAttribute("bankName");
        Bank bank = new Bank();
        bank.setName(bankName);
        try {
            List<Bank> bankList = bankDAO.queryByModel(bank);
            if(ListUtils.isEmpty(bankList)){
                throw new BusinessException("未选择银行");
            }
            Bank b = bankList.get(0);
            BankCard bc = new BankCard();
            bc.setBankId(b.getId());
            bc.setCardCode((String)httpSession.getAttribute("cardCode"));
            Cust cust = (Cust)httpSession.getAttribute("user");
            Integer custId = cust.getId();
            bc.setCustId(custId);

            bc.setBindDate(new Date());

            bankCardDAO.insert(bc);
            httpSession.removeAttribute("bankName");
            httpSession.removeAttribute("cardCode");
        } catch (Exception e) {
            e.printStackTrace();
        }
        mv.addObject("menuFlag", "bindnewcardfour");
        return mv;
    }

    //选择银行
    @RequestMapping(value = "bindnewcardchoose", method = RequestMethod.GET)
    public ModelAndView BindNewCardFive() throws BusinessException {
        ModelAndView mv = new ModelAndView("bindnewcardchoose");
        try {
            List<Bank> list = bankDAO.queryByModel(new Bank());
            mv.addObject("bankList", list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }

    //选择银行成功之后  将银行名称保存到session
    @RequestMapping(value = "bankchooseover", method = RequestMethod.POST)
    public BaseResp BindNewCardSix(@RequestBody Bank bank) throws BusinessException {
        BaseResp resp = new BaseResp();
        httpSession.setAttribute("bankName",bank.getName());

        return resp;
    }




    //解除绑定银行卡
    //将需要删除的银行卡保存到session
    @RequestMapping(value = "unbindbankcard", method = RequestMethod.GET)
    public ModelAndView unBindBankCardOne(@RequestParam Integer id) throws BusinessException {
        ModelAndView mv = new ModelAndView("unbindbankcard");
        try {
            BankCard unBindCard = bankCardDAO.queryById(id);
            httpSession.setAttribute("unBindCard",unBindCard);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }


    //验证密码 成功 解除绑定
    @RequestMapping(value = "checktradepassword", method = RequestMethod.POST)
    public BaseResp unBindBankCardTwo(@RequestBody Cust cust) throws BusinessException {
        BaseResp resp = new BaseResp();
        try {
            if(cust.getTradePassword() == ""){
                throw new BusinessException("交易密码不能为空");
            }

            Cust user = (Cust)httpSession.getAttribute("user");

            if(!(cust.getTradePassword().equals(user.getTradePassword()))){
                throw new BusinessException("输入交易密码错误");
            }

            BankCard bankCard = (BankCard) httpSession.getAttribute("unBindCard");
            bankCardDAO.deleteById(bankCard.getId());
            httpSession.removeAttribute("unBindCard");

        } catch (Exception ex) {
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }

    //解绑银行卡成功
    @RequestMapping(value = "unbindsuccess", method = RequestMethod.GET)
    public ModelAndView unBindSuccess() throws BusinessException {
        ModelAndView mv = new ModelAndView("unbindsuccess");

        return mv;
    }
}
